/*
 * @Author: liuqiang
 * @Date: 2021-02-20 18:07:36
 * @Last Modified by: liuqiang
 * @Last Modified time: 2021-03-02 14:53:25
 */
const { addBill } = require('../models/bill');
const {
  createCustomer,
  findCustomer,
  updateCustomer,
} = require('../models/customer');
const { isRequiredParams } = require('../utils/tool');

const UserCtl = {
  // 新增用户
  create: async (ctx) => {
    const body = ctx.request.body;
    const { err, msg } = isRequiredParams(['name', 'phone'], body);
    if (err) {
      ctx.body = {
        err: -1,
        msg,
      };
      return;
    }
    let res = await createCustomer(body);
    if (res.err) {
      ctx.body = {
        err: -999,
        msg: '创建失败！',
        data: {},
      };
      return;
    }
    ctx.body = {
      err: 0,
      msg: '创建成功',
      data: res,
    };
  },
  // 查询用户
  find: async (ctx) => {
    const params = ctx.query;
    let res = await findCustomer(params);
    if (res.err) {
      ctx.body = {
        err: -999,
        msg: '查询失败！',
        data: [],
      };
      return;
    }
    ctx.body = {
      err: 0,
      msg: '查询成功',
      data: res,
    };
  },
  // 修改用户信息
  update: async (ctx) => {
    const body = ctx.request.body;
    const { err, msg } = isRequiredParams(['id'], body);
    if (err) {
      ctx.body = {
        err: -1,
        msg,
      };
      return;
    }
    const res = await updateCustomer(body);
    if (res.err) {
      ctx.body = {
        err: -999,
        msg: '修改用户信息失败！',
        data: {},
      };
      return;
    }
    ctx.body = {
      err: 0,
      msg: '修改用户信息成功',
      data: res,
    };
  },
  // 用户消费
  consume: async (ctx) => {
    const body = ctx.request.body;
    const { err, msg } = isRequiredParams(['id', 'price'], body);
    if (err) {
      ctx.body = {
        err: -1,
        msg,
      };
      return;
    }
    const { id, price, remark } = body;
    if (price <= 0) {
      ctx.body = {
        err: -1,
        msg: '消费金额必须大于0',
      };
      return;
    }
    let customer = await findCustomer({ id });
    customer = customer.length > 0 ? customer[0] : {};
    if (!customer.id) {
      ctx.body = {
        err: -9,
        msg: '新增消费失败，不存在该用户',
      };
      return;
    }

    const pay = customer.pay + 1; // 消费次数加一
    const count = customer.count - price; // 余额

    if (count < 0) {
      ctx.body = {
        err: -2,
        msg: '余额不足，扣费失败',
      };
      return;
    }

    const res = await updateCustomer({
      id: customer.id,
      pay,
      count,
    });
    if (res.err) {
      ctx.body = {
        err: -999,
        msg: '┗|｀O′|┛ 嗷~~更新消费失败啦！',
        data: {},
      };
      return;
    }

    // 添加一条账单记录
    const bill = await addBill({
      customerNo: customer.no,
      customerId: customer.id,
      customerName: customer.name,
      customerPhone: customer.phone,
      customerSex: customer.sex,
      customerCount: count,
      customerVip: customer.vip,
      price: price,
      remark: remark,
    });

    if (bill.err) {
      ctx.body = {
        err: -999,
        msg: '┗|｀O′|┛ 嗷~~更新消费失败啦！',
        data: {},
      };
      return;
    }

    ctx.body = {
      err: 0,
      msg: '新增消费成功',
      data: {
        customer: res,
        bill,
      },
    };
  },
};

module.exports = UserCtl;
